package com.springboot.shanhexin.controller;

import com.springboot.shanhexin.entity.Page;
import com.springboot.shanhexin.entity.my_hotel;
import com.springboot.shanhexin.service.my_hotelService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class my_hotelCtrl {

    @Autowired
    private my_hotelService my_hotelService;

    //分页显示全部
    @RequestMapping("/my_hotel")
    public String my_hotel(Model model, String pageNow, String nosearch) {
        int totalCount = 0;
        totalCount = my_hotelService.getCount();
        List<my_hotel> my_hotelList=new ArrayList<my_hotel>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            my_hotelList=my_hotelService.findByPage(page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            my_hotelList=my_hotelService.findByPage(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("page", page);
        model.addAttribute("top", "top_tourist");
        model.addAttribute("show_left", "left_my_hotel");
        model.addAttribute("left", "left_y_tourist");
        model.addAttribute("mypage", "my_hotel");
        model.addAttribute("my_hotel", my_hotelList);
        if(nosearch != null){
            model.addAttribute("nosearch", nosearch);
        }
        return "houtai_my_hotel";
    }

    //分页显示全部搜索
    @RequestMapping("/searchmy_hotel")
    public String searchmy_hotel(Model model, String pageNow,int types,String keyword) {
        int totalCount = 0;
        String keyword_new="b.name";
        if(types == 1) {
            keyword_new = "b.name";
        }else if (types == 2) {
            keyword_new = "a.title";
        }else if (types == 3) {
            keyword_new = "c.name";
        }
        totalCount = my_hotelService.getsearchCount(keyword_new,keyword);
        List<my_hotel> my_hotelList=new ArrayList<my_hotel>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            my_hotelList=my_hotelService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            my_hotelList=my_hotelService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }
        if(my_hotelList != null && totalCount != 0){
            model.addAttribute("page", page);
            model.addAttribute("top", "top_tourist");
            model.addAttribute("show_left", "left_my_hotel");
            model.addAttribute("left", "left_y_tourist");
            model.addAttribute("mypage", "search&types="+types+"&keyword="+keyword+"");
            model.addAttribute("my_hotel", my_hotelList);
            return "houtai_my_hotel";
        }else{
            model.addAttribute("nosearch", "nosearch");
            return "redirect:/my_hotel?nosearch=nosearch";
        }
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/addmy_hotel")
    @ResponseBody
    public String addmy_hotel(my_hotel my_hotel) {
        my_hotelService.add(my_hotel);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //更改
    @RequestMapping("/updatemy_hotel")
    public String updatemy_hotel(my_hotel my_hotel) {
        my_hotelService.update(my_hotel);
        return "redirect:/my_hotel";
    }

    //删除
    @RequestMapping("/deletemy_hotel")
    public String deletemy_hotel(int userid) {
        my_hotelService.delete(userid);
        return "redirect:/my_hotel";
    }

    //跳转到更改页面
    @RequestMapping("/to_updatemy_hotel")
    public String to_updatemy_hotel(Model model, int userid) {
        List<my_hotel> my_hotelList=new ArrayList<my_hotel>();
        my_hotelList = my_hotelService.getUserone(userid);
        model.addAttribute("top", "top_tourist");
        model.addAttribute("show_left", "left_updatemy_hotel");
        model.addAttribute("left", "left_y_tourist");
        model.addAttribute("mypage", "my_hotel");
        model.addAttribute("my_hotel", my_hotelList);
        return "houtai_updatemy_hotel";
    }

    //跳转到添加页面
    @RequestMapping("/to_addmy_hotel")
    public String to_addmy_hotel(Model model) {
        model.addAttribute("top", "top_tourist");
        model.addAttribute("show_left", "left_addmy_hotel");
        model.addAttribute("left", "left_y_tourist");
        model.addAttribute("mypage", "my_hotel");
        return "houtai_addmy_hotel";
    }

}
